{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 项目背景\n",
    "电商会在618,双11等进行大量促销活动来吸引买家，但很多被吸引的买家都是一次性交易或者对运营活动有依赖(有活动就来，没活动就不来)，这些促销对长期销售的影响有限。本项目通过特征工程，从基础电商数据提取出有效特征形成训练数据集，再结合机器学习的建模融合，最终对用户是否会重复购买进行预测，便于商家识别哪些用户可以成为重复购买的忠实买家，从而降低促销成本，提高用户生命周期价值."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  理解数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 用户信息表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>376517</td>\n",
       "      <td>6.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>234512</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>344532</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>186135</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>30230</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  age_range  gender\n",
       "0   376517        6.0     1.0\n",
       "1   234512        5.0     0.0\n",
       "2   344532        5.0     0.0\n",
       "3   186135        5.0     0.0\n",
       "4    30230        5.0     0.0"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import stats\n",
    "\n",
    "user_info = pd.read_csv(r\"data\\user_info.csv\")\n",
    "user_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>424170</td>\n",
       "      <td>424170</td>\n",
       "      <td>424170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>424170</td>\n",
       "      <td>10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>10535</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>1</td>\n",
       "      <td>111654</td>\n",
       "      <td>285638</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_id age_range  gender\n",
       "count   424170    424170  424170\n",
       "unique  424170        10       4\n",
       "top      10535       3.0     0.0\n",
       "freq         1    111654  285638"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.user_id = user_info.user_id.astype(\"str\")\n",
    "user_info.age_range = user_info.age_range.astype(\"str\")\n",
    "user_info.gender = user_info.gender.astype(\"str\")\n",
    "user_info.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 223,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 424170 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    424170 non-null  int64  \n",
      " 1   age_range  421953 non-null  float64\n",
      " 2   gender     417734 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 9.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.user_id = user_info.user_id.astype(\"int64\")\n",
    "user_info.age_range = user_info.age_range.astype(\"float\")\n",
    "user_info.gender = user_info.gender.astype(\"float\")\n",
    "user_info.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除缺失值\n",
    "user_info.dropna(axis=0,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 417708 entries, 0 to 424169\n",
      "Data columns (total 3 columns):\n",
      " #   Column     Non-Null Count   Dtype  \n",
      "---  ------     --------------   -----  \n",
      " 0   user_id    417708 non-null  int64  \n",
      " 1   age_range  417708 non-null  float64\n",
      " 2   gender     417708 non-null  float64\n",
      "dtypes: float64(2), int64(1)\n",
      "memory usage: 12.7 MB\n"
     ]
    }
   ],
   "source": [
    "user_info.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.duplicated().sum()\n",
    "#user_info.drop_duplicates(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0    285634\n",
       "1.0    121655\n",
       "2.0     10419\n",
       "Name: gender, dtype: int64"
      ]
     },
     "execution_count": 227,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.gender.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0    110952\n",
       "0.0     90638\n",
       "4.0     79649\n",
       "2.0     52420\n",
       "5.0     40601\n",
       "6.0     35257\n",
       "7.0      6924\n",
       "8.0      1243\n",
       "1.0        24\n",
       "Name: age_range, dtype: int64"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_info.age_range.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除缺失值 gender字段的2为缺失值，　age_range 的0也为缺失值 \n",
    "user_info = user_info.loc[(user_info.gender != 2) & (user_info.age_range != 0),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_info.age_range[user_info.age_range == 8] =7"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  用户商铺复购表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>34176</td>\n",
       "      <td>3906</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>34176</td>\n",
       "      <td>121</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>34176</td>\n",
       "      <td>4356</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>34176</td>\n",
       "      <td>2217</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>230784</td>\n",
       "      <td>4818</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label\n",
       "0    34176         3906      0\n",
       "1    34176          121      0\n",
       "2    34176         4356      1\n",
       "3    34176         2217      0\n",
       "4   230784         4818      0"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant = pd.read_csv(r\"data\\user_merchant.csv\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    244912\n",
       "1     15952\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看标签比例\n",
    "user_merchant.label.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06115063788027478"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "15952/user_merchant.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 260864 entries, 0 to 260863\n",
      "Data columns (total 3 columns):\n",
      " #   Column       Non-Null Count   Dtype\n",
      "---  ------       --------------   -----\n",
      " 0   user_id      260864 non-null  int64\n",
      " 1   merchant_id  260864 non-null  int64\n",
      " 2   label        260864 non-null  int64\n",
      "dtypes: int64(3)\n",
      "memory usage: 6.0 MB\n"
     ]
    }
   ],
   "source": [
    "user_merchant.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>260864</td>\n",
       "      <td>260864</td>\n",
       "      <td>260864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>212062</td>\n",
       "      <td>1993</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>221133</td>\n",
       "      <td>4044</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>18</td>\n",
       "      <td>3379</td>\n",
       "      <td>244912</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_id merchant_id   label\n",
       "count   260864      260864  260864\n",
       "unique  212062        1993       2\n",
       "top     221133        4044       0\n",
       "freq        18        3379  244912"
      ]
     },
     "execution_count": 234,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant.user_id = user_merchant.user_id.astype(\"str\")\n",
    "user_merchant.merchant_id  = user_merchant.merchant_id .astype(\"str\")\n",
    "user_merchant.label  = user_merchant.label .astype(\"str\")\n",
    "user_merchant.describe(include=\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 235,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>260864</td>\n",
       "      <td>260864</td>\n",
       "      <td>260864</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>212062</td>\n",
       "      <td>1993</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>221133</td>\n",
       "      <td>4044</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>18</td>\n",
       "      <td>3379</td>\n",
       "      <td>244912</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       user_id merchant_id   label\n",
       "count   260864      260864  260864\n",
       "unique  212062        1993       2\n",
       "top     221133        4044       0\n",
       "freq        18        3379  244912"
      ]
     },
     "execution_count": 235,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant.describe(include=\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>251038</td>\n",
       "      <td>3624</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  user_id merchant_id label\n",
       "0  251021        3609     0\n",
       "1  251038        3624     0\n",
       "2  314568        3828     0\n",
       "3  314580        3853     0\n",
       "4  339322        1399     0"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#少类小于10%，我们就认为是不平衡数据了\n",
    "from imblearn.under_sampling  import NearMiss\n",
    "ee =NearMiss(version=1)\n",
    "X_resampled, y_resampled = ee.fit_sample(user_merchant.loc[:,[\"user_id\",\"merchant_id\"]], user_merchant.label)\n",
    "user_merchant =pd.concat([X_resampled,y_resampled],axis=1)\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 237,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 31904 entries, 0 to 31903\n",
      "Data columns (total 3 columns):\n",
      " #   Column       Non-Null Count  Dtype\n",
      "---  ------       --------------  -----\n",
      " 0   user_id      31904 non-null  int64\n",
      " 1   merchant_id  31904 non-null  int64\n",
      " 2   label        31904 non-null  int64\n",
      "dtypes: int64(3)\n",
      "memory usage: 747.9 KB\n"
     ]
    }
   ],
   "source": [
    "user_merchant.user_id = user_merchant.user_id.astype(\"int64\")\n",
    "user_merchant.merchant_id  = user_merchant.merchant_id .astype(\"int64\")\n",
    "user_merchant.label  = user_merchant.label .astype(\"int64\")\n",
    "user_merchant.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1    15952\n",
       "0    15952\n",
       "Name: label, dtype: int64"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_resampled.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 239,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 利用用户商铺表失衡数据处理的结果过滤用户信息表\n",
    "user_info = user_info.loc[user_info.user_id.isin(user_merchant.user_id.unique()),:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  用户行为表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log = pd.read_csv(r\"data\\user_log.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>328862</td>\n",
       "      <td>323294</td>\n",
       "      <td>833</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>328862</td>\n",
       "      <td>844400</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>328862</td>\n",
       "      <td>575153</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>328862</td>\n",
       "      <td>996875</td>\n",
       "      <td>1271</td>\n",
       "      <td>2882</td>\n",
       "      <td>2661.0</td>\n",
       "      <td>829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>328862</td>\n",
       "      <td>1086186</td>\n",
       "      <td>1271</td>\n",
       "      <td>1253</td>\n",
       "      <td>1049.0</td>\n",
       "      <td>829</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type\n",
       "0   328862   323294     833       2882    2661.0         829            0\n",
       "1   328862   844400    1271       2882    2661.0         829            0\n",
       "2   328862   575153    1271       2882    2661.0         829            0\n",
       "3   328862   996875    1271       2882    2661.0         829            0\n",
       "4   328862  1086186    1271       1253    1049.0         829            0"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 242,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 利用用户商铺表失衡数据处理的结果过滤用户行为表\n",
    "user_log = user_log.loc[user_log.user_id.isin(user_merchant.user_id.values),:]\n",
    "user_log = user_log.loc[user_log.seller_id.isin(user_merchant.merchant_id.values),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Int64Index: 1972402 entries, 1691 to 54924616\n",
      "Data columns (total 7 columns):\n",
      " #   Column       Dtype  \n",
      "---  ------       -----  \n",
      " 0   user_id      int64  \n",
      " 1   item_id      int64  \n",
      " 2   cat_id       int64  \n",
      " 3   seller_id    int64  \n",
      " 4   brand_id     float64\n",
      " 5   time_stamp   int64  \n",
      " 6   action_type  int64  \n",
      "dtypes: float64(1), int64(6)\n",
      "memory usage: 120.4 MB\n"
     ]
    }
   ],
   "source": [
    "user_log.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "      <td>1972402</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>unique</th>\n",
       "      <td>30629</td>\n",
       "      <td>193324</td>\n",
       "      <td>1180</td>\n",
       "      <td>1912</td>\n",
       "      <td>3358</td>\n",
       "      <td>185</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>top</th>\n",
       "      <td>391188</td>\n",
       "      <td>269715</td>\n",
       "      <td>662</td>\n",
       "      <td>3828</td>\n",
       "      <td>1446.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>freq</th>\n",
       "      <td>3954</td>\n",
       "      <td>4473</td>\n",
       "      <td>182958</td>\n",
       "      <td>76723</td>\n",
       "      <td>76649</td>\n",
       "      <td>440218</td>\n",
       "      <td>1732606</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        user_id  item_id   cat_id seller_id brand_id time_stamp action_type\n",
       "count   1972402  1972402  1972402   1972402  1972402    1972402     1972402\n",
       "unique    30629   193324     1180      1912     3358        185           4\n",
       "top      391188   269715      662      3828   1446.0       1111           0\n",
       "freq       3954     4473   182958     76723    76649     440218     1732606"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.user_id = user_log.user_id.astype(\"str\")\n",
    "user_log.item_id = user_log.item_id.astype(\"str\")\n",
    "user_log.cat_id = user_log.cat_id.astype(\"str\")\n",
    "user_log.seller_id = user_log.seller_id.astype(\"str\")\n",
    "user_log.brand_id = user_log.brand_id.astype(\"str\")\n",
    "user_log.time_stamp = user_log.time_stamp.astype(\"str\")\n",
    "user_log.action_type = user_log.action_type.astype(\"str\")\n",
    "user_log.describe(include=\"all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_log.user_id = user_log.user_id.astype(\"int64\")\n",
    "user_log.item_id = user_log.item_id.astype(\"int64\")\n",
    "user_log.cat_id = user_log.cat_id.astype(\"int64\")\n",
    "user_log.seller_id = user_log.seller_id.astype(\"int64\")\n",
    "user_log.brand_id = user_log.brand_id.astype(\"float\")\n",
    "user_log.time_stamp = user_log.time_stamp.astype(\"int64\")\n",
    "user_log.action_type = user_log.action_type.astype(\"int64\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id                            [0, 0.0]\n",
       "item_id                            [0, 0.0]\n",
       "cat_id                             [0, 0.0]\n",
       "seller_id                          [0, 0.0]\n",
       "brand_id       [2938, 0.001489554360622226]\n",
       "time_stamp                         [0, 0.0]\n",
       "action_type                        [0, 0.0]\n",
       "dtype: object"
      ]
     },
     "execution_count": 246,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算缺失值，及趋缺失占比\n",
    "user_log.apply(lambda x:[x.isnull().sum(),x.isnull().sum()/x.size], axis=0)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除缺失值\n",
    "user_log = user_log[user_log.brand_id.isna()==False]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 248,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1969464, 7)"
      ]
     },
     "execution_count": 248,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据准备\n",
    "###  用户特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>action_type</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         action_type\n",
       "user_id             \n",
       "1                 21\n",
       "4                 42\n",
       "7                  6\n",
       "14               313\n",
       "17                23"
      ]
     },
     "execution_count": 249,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户交互总次数\n",
    "user_feaut = user_log.groupby(\"user_id\")[\"action_type\"].count().to_frame()\n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>action_type</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "action_type      0   1     2    3\n",
       "user_id                          \n",
       "1             16.0 NaN   5.0  NaN\n",
       "4             41.0 NaN   1.0  NaN\n",
       "7              2.0 NaN   4.0  NaN\n",
       "14           290.0 NaN  18.0  5.0\n",
       "17            18.0 NaN   1.0  4.0"
      ]
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户各种行为总次数统计（点击、加购、收藏和购买）\n",
    "user_feaut_2 =  pd.pivot_table(user_log,index=\"user_id\",columns=\"action_type\",values=\"cat_id\",aggfunc=\"count\")\n",
    "user_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>action_type</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         action_type      0   1     2    3\n",
       "user_id                                   \n",
       "1                 21   16.0 NaN   5.0  NaN\n",
       "4                 42   41.0 NaN   1.0  NaN\n",
       "7                  6    2.0 NaN   4.0  NaN\n",
       "14               313  290.0 NaN  18.0  5.0\n",
       "17                23   18.0 NaN   1.0  4.0"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut = user_feaut.merge(user_feaut_2,on=\"user_id\")\n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_log</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         total_log  click  add_car   buy  collect\n",
       "user_id                                          \n",
       "1               21   16.0      NaN   5.0      NaN\n",
       "4               42   41.0      NaN   1.0      NaN\n",
       "7                6    2.0      NaN   4.0      NaN\n",
       "14             313  290.0      NaN  18.0      5.0\n",
       "17              23   18.0      NaN   1.0      4.0"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut.columns =[\"total_log\",\"click\",\"add_car\",\"buy\",\"collect\"]\n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>brand_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>40</td>\n",
       "      <td>177</td>\n",
       "      <td>34</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         seller_id  item_id  cat_id  brand_id\n",
       "user_id                                      \n",
       "1                4        4       3         4\n",
       "4                6       20      10         6\n",
       "7                2        5       3         2\n",
       "14              40      177      34        38\n",
       "17               4        8       5         4"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户交互了多少商铺数多少种商品，多少商品类别和多少商品品牌数量\n",
    "user_feaut_2 = user_log.groupby(\"user_id\")[\"seller_id\",\"item_id\",\"cat_id\",\"brand_id\"].nunique()\n",
    "user_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 254,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>40</td>\n",
       "      <td>177</td>\n",
       "      <td>34</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         seller_count  item_count  cat_count  brand_count\n",
       "user_id                                                  \n",
       "1                   4           4          3            4\n",
       "4                   6          20         10            6\n",
       "7                   2           5          3            2\n",
       "14                 40         177         34           38\n",
       "17                  4           8          5            4"
      ]
     },
     "execution_count": 254,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut_2.columns = [\"seller_count\",\"item_count\",\"cat_count\",\"brand_count\"]\n",
    "user_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_log</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>40</td>\n",
       "      <td>177</td>\n",
       "      <td>34</td>\n",
       "      <td>38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         total_log  click  add_car   buy  collect  seller_count  item_count  \\\n",
       "user_id                                                                       \n",
       "1               21   16.0      NaN   5.0      NaN             4           4   \n",
       "4               42   41.0      NaN   1.0      NaN             6          20   \n",
       "7                6    2.0      NaN   4.0      NaN             2           5   \n",
       "14             313  290.0      NaN  18.0      5.0            40         177   \n",
       "17              23   18.0      NaN   1.0      4.0             4           8   \n",
       "\n",
       "         cat_count  brand_count  \n",
       "user_id                          \n",
       "1                3            4  \n",
       "4               10            6  \n",
       "7                3            2  \n",
       "14              34           38  \n",
       "17               5            4  "
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut = user_feaut.merge(user_feaut_2,on=\"user_id\") \n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {},
   "outputs": [],
   "source": [
    "#从时间戳中提月和日\n",
    "user_log[\"month\"] = user_log.time_stamp // 100;\n",
    "user_log[\"day\"] = user_log.time_stamp % 100;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1691</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1692</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1693</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1694</th>\n",
       "      <td>26516</td>\n",
       "      <td>352345</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1718</th>\n",
       "      <td>26516</td>\n",
       "      <td>142231</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type  \\\n",
       "1691    26516   416965    1401        586    5579.0        1107            0   \n",
       "1692    26516   416965    1401        586    5579.0        1107            0   \n",
       "1693    26516   416965    1401        586    5579.0        1107            0   \n",
       "1694    26516   352345     177       2565    8149.0        1101            0   \n",
       "1718    26516   142231     177       2565    8149.0        1101            0   \n",
       "\n",
       "      month  day  \n",
       "1691     11    7  \n",
       "1692     11    7  \n",
       "1693     11    7  \n",
       "1694     11    1  \n",
       "1718     11    1  "
      ]
     },
     "execution_count": 257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>month_count</th>\n",
       "      <th>day_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         month_count  day_count\n",
       "user_id                        \n",
       "1                  2          3\n",
       "4                  5          9\n",
       "7                  2          3\n",
       "14                 7         48\n",
       "17                 3          6"
      ]
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用户平均每天交互、购买的次数 ，用户平均每月交互、购买的次数\n",
    "user_feaut_2=user_log.groupby(\"user_id\")[\"month\",\"time_stamp\"].nunique()\n",
    "user_feaut_2.columns = [\"month_count\",\"day_count\"]\n",
    "user_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_log</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month_count</th>\n",
       "      <th>day_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>40</td>\n",
       "      <td>177</td>\n",
       "      <td>34</td>\n",
       "      <td>38</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         total_log  click  add_car   buy  collect  seller_count  item_count  \\\n",
       "user_id                                                                       \n",
       "1               21   16.0      NaN   5.0      NaN             4           4   \n",
       "4               42   41.0      NaN   1.0      NaN             6          20   \n",
       "7                6    2.0      NaN   4.0      NaN             2           5   \n",
       "14             313  290.0      NaN  18.0      5.0            40         177   \n",
       "17              23   18.0      NaN   1.0      4.0             4           8   \n",
       "\n",
       "         cat_count  brand_count  month_count  day_count  \n",
       "user_id                                                  \n",
       "1                3            4            2          3  \n",
       "4               10            6            5          9  \n",
       "7                3            2            2          3  \n",
       "14              34           38            7         48  \n",
       "17               5            4            3          6  "
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut = user_feaut.merge(user_feaut_2,on=\"user_id\") \n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 260,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_log</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month_count</th>\n",
       "      <th>day_count</th>\n",
       "      <th>month_avg_log</th>\n",
       "      <th>month_avg_buy</th>\n",
       "      <th>day_avg_log</th>\n",
       "      <th>day_avg_buy</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>21</td>\n",
       "      <td>16.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>10.500000</td>\n",
       "      <td>2.500000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>1.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>42</td>\n",
       "      <td>41.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>20</td>\n",
       "      <td>10</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>8.400000</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>4.666667</td>\n",
       "      <td>0.111111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>1.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>313</td>\n",
       "      <td>290.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>18.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>40</td>\n",
       "      <td>177</td>\n",
       "      <td>34</td>\n",
       "      <td>38</td>\n",
       "      <td>7</td>\n",
       "      <td>48</td>\n",
       "      <td>44.714286</td>\n",
       "      <td>2.571429</td>\n",
       "      <td>6.520833</td>\n",
       "      <td>0.375000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>23</td>\n",
       "      <td>18.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>7.666667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>3.833333</td>\n",
       "      <td>0.166667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         total_log  click  add_car   buy  collect  seller_count  item_count  \\\n",
       "user_id                                                                       \n",
       "1               21   16.0      NaN   5.0      NaN             4           4   \n",
       "4               42   41.0      NaN   1.0      NaN             6          20   \n",
       "7                6    2.0      NaN   4.0      NaN             2           5   \n",
       "14             313  290.0      NaN  18.0      5.0            40         177   \n",
       "17              23   18.0      NaN   1.0      4.0             4           8   \n",
       "\n",
       "         cat_count  brand_count  month_count  day_count  month_avg_log  \\\n",
       "user_id                                                                  \n",
       "1                3            4            2          3      10.500000   \n",
       "4               10            6            5          9       8.400000   \n",
       "7                3            2            2          3       3.000000   \n",
       "14              34           38            7         48      44.714286   \n",
       "17               5            4            3          6       7.666667   \n",
       "\n",
       "         month_avg_buy  day_avg_log  day_avg_buy  \n",
       "user_id                                           \n",
       "1             2.500000     7.000000     1.666667  \n",
       "4             0.200000     4.666667     0.111111  \n",
       "7             2.000000     2.000000     1.333333  \n",
       "14            2.571429     6.520833     0.375000  \n",
       "17            0.333333     3.833333     0.166667  "
      ]
     },
     "execution_count": 260,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_feaut[\"month_avg_log\"] = user_feaut.total_log/user_feaut.month_count\n",
    "user_feaut[\"month_avg_buy\"] =np.where(user_feaut.buy.isna(),0,user_feaut.buy/user_feaut.month_count)\n",
    "user_feaut[\"day_avg_log\"] = user_feaut.total_log/user_feaut.day_count\n",
    "user_feaut[\"day_avg_buy\"] =np.where(user_feaut.buy.isna(),0,user_feaut.buy/user_feaut.day_count)\n",
    "user_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 261,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除特征提取的临时数据框\n",
    "del user_feaut_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 商铺特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           user_id\n",
       "seller_id         \n",
       "2              214\n",
       "8              305\n",
       "9              242\n",
       "10            1658\n",
       "13             681"
      ]
     },
     "execution_count": 262,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺下所有交互总次数\n",
    "shop_feaut = user_log.groupby(\"seller_id\")[\"user_id\"].count().to_frame()\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count\n",
       "seller_id             \n",
       "2                  214\n",
       "8                  305\n",
       "9                  242\n",
       "10                1658\n",
       "13                 681"
      ]
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut.columns=[\"total_count\"]\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>action_type</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "action_type       0   1      2     3\n",
       "seller_id                           \n",
       "2             181.0 NaN   23.0  10.0\n",
       "8             258.0 NaN   30.0  17.0\n",
       "9             219.0 NaN   14.0   9.0\n",
       "10           1529.0 NaN   72.0  57.0\n",
       "13            567.0 NaN  100.0  14.0"
      ]
     },
     "execution_count": 264,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺下各种行为总次数统计（点击、加购、收藏和购买）\n",
    "shop_feaut_2 = pd.pivot_table(user_log,index=\"seller_id\",columns=\"action_type\",values=\"user_id\",aggfunc=\"count\")\n",
    "shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count       0   1      2     3\n",
       "seller_id                                      \n",
       "2                  214   181.0 NaN   23.0  10.0\n",
       "8                  305   258.0 NaN   30.0  17.0\n",
       "9                  242   219.0 NaN   14.0   9.0\n",
       "10                1658  1529.0 NaN   72.0  57.0\n",
       "13                 681   567.0 NaN  100.0  14.0"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut= shop_feaut.merge(shop_feaut_2,on=\"seller_id\")\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect\n",
       "seller_id                                              \n",
       "2                  214   181.0      NaN   23.0     10.0\n",
       "8                  305   258.0      NaN   30.0     17.0\n",
       "9                  242   219.0      NaN   14.0      9.0\n",
       "10                1658  1529.0      NaN   72.0     57.0\n",
       "13                 681   567.0      NaN  100.0     14.0"
      ]
     },
     "execution_count": 266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut.columns = [\"total_count\",\"click\",\"add_car\",\"buy\",\"collect\"]\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>brand_id</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           user_id  item_id  cat_id  brand_id\n",
       "seller_id                                    \n",
       "2               82       48       7         1\n",
       "8               83       51       8         1\n",
       "9               72      103      13        15\n",
       "10             410      185      13         2\n",
       "13             187       28       5         1"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺下交互的总用户数，多少被交互的商品，商品类别和商品品牌数量\n",
    "shop_feaut_2 = user_log.groupby(\"seller_id\")[\"user_id\",\"item_id\",\"cat_id\",\"brand_id\"].nunique()\n",
    "shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>user_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect  user_count  \\\n",
       "seller_id                                                             \n",
       "2                  214   181.0      NaN   23.0     10.0          82   \n",
       "8                  305   258.0      NaN   30.0     17.0          83   \n",
       "9                  242   219.0      NaN   14.0      9.0          72   \n",
       "10                1658  1529.0      NaN   72.0     57.0         410   \n",
       "13                 681   567.0      NaN  100.0     14.0         187   \n",
       "\n",
       "           item_count  cat_count  brand_count  \n",
       "seller_id                                      \n",
       "2                  48          7            1  \n",
       "8                  51          8            1  \n",
       "9                 103         13           15  \n",
       "10                185         13            2  \n",
       "13                 28          5            1  "
      ]
     },
     "execution_count": 268,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut_2.columns=[\"user_count\",\"item_count\",\"cat_count\",\"brand_count\"]\n",
    "shop_feaut= shop_feaut.merge(shop_feaut_2,on=\"seller_id\")\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>user_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect  user_count  \\\n",
       "seller_id                                                             \n",
       "2                  214   181.0      NaN   23.0     10.0          82   \n",
       "8                  305   258.0      NaN   30.0     17.0          83   \n",
       "9                  242   219.0      NaN   14.0      9.0          72   \n",
       "10                1658  1529.0      NaN   72.0     57.0         410   \n",
       "13                 681   567.0      NaN  100.0     14.0         187   \n",
       "\n",
       "           item_count  cat_count  brand_count  month  \n",
       "seller_id                                             \n",
       "2                  48          7            1      7  \n",
       "8                  51          8            1      7  \n",
       "9                 103         13           15      7  \n",
       "10                185         13            2      7  \n",
       "13                 28          5            1      7  "
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺月平均有多少用户交互\n",
    "shop_feaut_2= user_log.groupby(\"seller_id\")[\"month\"].nunique()\n",
    "shop_feaut= shop_feaut.merge(shop_feaut_2,on=\"seller_id\")\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>user_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month</th>\n",
       "      <th>month_avg_user</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.714286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.857143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "      <td>7</td>\n",
       "      <td>10.285714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>58.571429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>26.714286</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect  user_count  \\\n",
       "seller_id                                                             \n",
       "2                  214   181.0      NaN   23.0     10.0          82   \n",
       "8                  305   258.0      NaN   30.0     17.0          83   \n",
       "9                  242   219.0      NaN   14.0      9.0          72   \n",
       "10                1658  1529.0      NaN   72.0     57.0         410   \n",
       "13                 681   567.0      NaN  100.0     14.0         187   \n",
       "\n",
       "           item_count  cat_count  brand_count  month  month_avg_user  \n",
       "seller_id                                                             \n",
       "2                  48          7            1      7       11.714286  \n",
       "8                  51          8            1      7       11.857143  \n",
       "9                 103         13           15      7       10.285714  \n",
       "10                185         13            2      7       58.571429  \n",
       "13                 28          5            1      7       26.714286  "
      ]
     },
     "execution_count": 270,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut[\"month_avg_user\"]=shop_feaut.user_count/shop_feaut.month\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 285,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>26516</td>\n",
       "      <td>352345</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26516</td>\n",
       "      <td>142231</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type  \\\n",
       "0    26516   416965    1401        586    5579.0        1107            0   \n",
       "1    26516   416965    1401        586    5579.0        1107            0   \n",
       "2    26516   416965    1401        586    5579.0        1107            0   \n",
       "3    26516   352345     177       2565    8149.0        1101            0   \n",
       "4    26516   142231     177       2565    8149.0        1101            0   \n",
       "\n",
       "   month  day  age_range  gender  \n",
       "0     11    7        4.0     1.0  \n",
       "1     11    7        4.0     1.0  \n",
       "2     11    7        4.0     1.0  \n",
       "3     11    1        4.0     1.0  \n",
       "4     11    1        4.0     1.0  "
      ]
     },
     "execution_count": 285,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#获取用户详细信息\n",
    "user_log = user_log.merge(user_info,on=\"user_id\")\n",
    "user_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 286,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>age_range</th>\n",
       "      <th>1.0</th>\n",
       "      <th>2.0</th>\n",
       "      <th>3.0</th>\n",
       "      <th>4.0</th>\n",
       "      <th>5.0</th>\n",
       "      <th>6.0</th>\n",
       "      <th>7.0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>108.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "age_range  1.0   2.0    3.0   4.0   5.0   6.0   7.0\n",
       "seller_id                                          \n",
       "2          NaN  10.0   19.0  14.0   9.0   8.0   2.0\n",
       "8          NaN   6.0   23.0  17.0   8.0   7.0   2.0\n",
       "9          NaN   8.0   21.0  19.0   6.0  11.0   2.0\n",
       "10         NaN  17.0  108.0  80.0  55.0  40.0  10.0\n",
       "13         NaN  25.0   51.0  19.0  19.0  21.0   7.0"
      ]
     },
     "execution_count": 286,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺下交互的用户按年龄段和性别分别统计\n",
    "shop_feaut_2 = pd.pivot_table(user_log,index=\"seller_id\",columns=\"age_range\",values=\"user_id\",aggfunc=\"nunique\")\n",
    "shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>less18</th>\n",
       "      <th>between18and24</th>\n",
       "      <th>between25and29</th>\n",
       "      <th>between30and34</th>\n",
       "      <th>between35and39</th>\n",
       "      <th>between40and49</th>\n",
       "      <th>grate50</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>108.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           less18  between18and24  between25and29  between30and34  \\\n",
       "seller_id                                                           \n",
       "2             NaN            10.0            19.0            14.0   \n",
       "8             NaN             6.0            23.0            17.0   \n",
       "9             NaN             8.0            21.0            19.0   \n",
       "10            NaN            17.0           108.0            80.0   \n",
       "13            NaN            25.0            51.0            19.0   \n",
       "\n",
       "           between35and39  between40and49  grate50  \n",
       "seller_id                                           \n",
       "2                     9.0             8.0      2.0  \n",
       "8                     8.0             7.0      2.0  \n",
       "9                     6.0            11.0      2.0  \n",
       "10                   55.0            40.0     10.0  \n",
       "13                   19.0            21.0      7.0  "
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut_2.columns=[\"less18\",\"between18and24\",\"between25and29\",\"between30and34\",\"between35and39\",\"between40and49\",\"grate50\"]\n",
    "shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>user_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month</th>\n",
       "      <th>month_avg_user</th>\n",
       "      <th>less18</th>\n",
       "      <th>between18and24</th>\n",
       "      <th>between25and29</th>\n",
       "      <th>between30and34</th>\n",
       "      <th>between35and39</th>\n",
       "      <th>between40and49</th>\n",
       "      <th>grate50</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.714286</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.857143</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "      <td>7</td>\n",
       "      <td>10.285714</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>58.571429</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>108.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>26.714286</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>7.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect  user_count  \\\n",
       "seller_id                                                             \n",
       "2                  214   181.0      NaN   23.0     10.0          82   \n",
       "8                  305   258.0      NaN   30.0     17.0          83   \n",
       "9                  242   219.0      NaN   14.0      9.0          72   \n",
       "10                1658  1529.0      NaN   72.0     57.0         410   \n",
       "13                 681   567.0      NaN  100.0     14.0         187   \n",
       "\n",
       "           item_count  cat_count  brand_count  month  month_avg_user  less18  \\\n",
       "seller_id                                                                      \n",
       "2                  48          7            1      7       11.714286     NaN   \n",
       "8                  51          8            1      7       11.857143     NaN   \n",
       "9                 103         13           15      7       10.285714     NaN   \n",
       "10                185         13            2      7       58.571429     NaN   \n",
       "13                 28          5            1      7       26.714286     NaN   \n",
       "\n",
       "           between18and24  between25and29  between30and34  between35and39  \\\n",
       "seller_id                                                                   \n",
       "2                    10.0            19.0            14.0             9.0   \n",
       "8                     6.0            23.0            17.0             8.0   \n",
       "9                     8.0            21.0            19.0             6.0   \n",
       "10                   17.0           108.0            80.0            55.0   \n",
       "13                   25.0            51.0            19.0            19.0   \n",
       "\n",
       "           between40and49  grate50  \n",
       "seller_id                           \n",
       "2                     8.0      2.0  \n",
       "8                     7.0      2.0  \n",
       "9                    11.0      2.0  \n",
       "10                   40.0     10.0  \n",
       "13                   21.0      7.0  "
      ]
     },
     "execution_count": 288,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut= shop_feaut.merge(shop_feaut_2,on=\"seller_id\")\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 289,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>gender</th>\n",
       "      <th>0.0</th>\n",
       "      <th>1.0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>44.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>60.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>18.0</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>187.0</td>\n",
       "      <td>123.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>77.0</td>\n",
       "      <td>65.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "gender       0.0    1.0\n",
       "seller_id              \n",
       "2           44.0   18.0\n",
       "8           60.0    3.0\n",
       "9           18.0   49.0\n",
       "10         187.0  123.0\n",
       "13          77.0   65.0"
      ]
     },
     "execution_count": 289,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut_2 = pd.pivot_table(user_log,index=\"seller_id\",columns=\"gender\",values=\"user_id\",aggfunc=\"nunique\")\n",
    "shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 290,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total_count</th>\n",
       "      <th>click</th>\n",
       "      <th>add_car</th>\n",
       "      <th>buy</th>\n",
       "      <th>collect</th>\n",
       "      <th>user_count</th>\n",
       "      <th>item_count</th>\n",
       "      <th>cat_count</th>\n",
       "      <th>brand_count</th>\n",
       "      <th>month</th>\n",
       "      <th>month_avg_user</th>\n",
       "      <th>less18</th>\n",
       "      <th>between18and24</th>\n",
       "      <th>between25and29</th>\n",
       "      <th>between30and34</th>\n",
       "      <th>between35and39</th>\n",
       "      <th>between40and49</th>\n",
       "      <th>grate50</th>\n",
       "      <th>gender_F</th>\n",
       "      <th>gender_M</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>seller_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>214</td>\n",
       "      <td>181.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>82</td>\n",
       "      <td>48</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.714286</td>\n",
       "      <td>NaN</td>\n",
       "      <td>10.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>18.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>305</td>\n",
       "      <td>258.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>30.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>83</td>\n",
       "      <td>51</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>11.857143</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>8.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>60.0</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>242</td>\n",
       "      <td>219.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>72</td>\n",
       "      <td>103</td>\n",
       "      <td>13</td>\n",
       "      <td>15</td>\n",
       "      <td>7</td>\n",
       "      <td>10.285714</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>18.0</td>\n",
       "      <td>49.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1658</td>\n",
       "      <td>1529.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>72.0</td>\n",
       "      <td>57.0</td>\n",
       "      <td>410</td>\n",
       "      <td>185</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>58.571429</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17.0</td>\n",
       "      <td>108.0</td>\n",
       "      <td>80.0</td>\n",
       "      <td>55.0</td>\n",
       "      <td>40.0</td>\n",
       "      <td>10.0</td>\n",
       "      <td>187.0</td>\n",
       "      <td>123.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>681</td>\n",
       "      <td>567.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>14.0</td>\n",
       "      <td>187</td>\n",
       "      <td>28</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "      <td>26.714286</td>\n",
       "      <td>NaN</td>\n",
       "      <td>25.0</td>\n",
       "      <td>51.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>21.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>77.0</td>\n",
       "      <td>65.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           total_count   click  add_car    buy  collect  user_count  \\\n",
       "seller_id                                                             \n",
       "2                  214   181.0      NaN   23.0     10.0          82   \n",
       "8                  305   258.0      NaN   30.0     17.0          83   \n",
       "9                  242   219.0      NaN   14.0      9.0          72   \n",
       "10                1658  1529.0      NaN   72.0     57.0         410   \n",
       "13                 681   567.0      NaN  100.0     14.0         187   \n",
       "\n",
       "           item_count  cat_count  brand_count  month  month_avg_user  less18  \\\n",
       "seller_id                                                                      \n",
       "2                  48          7            1      7       11.714286     NaN   \n",
       "8                  51          8            1      7       11.857143     NaN   \n",
       "9                 103         13           15      7       10.285714     NaN   \n",
       "10                185         13            2      7       58.571429     NaN   \n",
       "13                 28          5            1      7       26.714286     NaN   \n",
       "\n",
       "           between18and24  between25and29  between30and34  between35and39  \\\n",
       "seller_id                                                                   \n",
       "2                    10.0            19.0            14.0             9.0   \n",
       "8                     6.0            23.0            17.0             8.0   \n",
       "9                     8.0            21.0            19.0             6.0   \n",
       "10                   17.0           108.0            80.0            55.0   \n",
       "13                   25.0            51.0            19.0            19.0   \n",
       "\n",
       "           between40and49  grate50  gender_F  gender_M  \n",
       "seller_id                                               \n",
       "2                     8.0      2.0      44.0      18.0  \n",
       "8                     7.0      2.0      60.0       3.0  \n",
       "9                    11.0      2.0      18.0      49.0  \n",
       "10                   40.0     10.0     187.0     123.0  \n",
       "13                   21.0      7.0      77.0      65.0  "
      ]
     },
     "execution_count": 290,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_feaut_2.columns=[\"gender_F\",\"gender_M\"]\n",
    "shop_feaut= shop_feaut.merge(shop_feaut_2,on=\"seller_id\")\n",
    "shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除提取商铺特征的临时数据框\n",
    "del shop_feaut_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 292,
   "metadata": {},
   "outputs": [],
   "source": [
    "#合并提取的特征到电商--用户行为复购表\n",
    "user_feaut = user_feaut.reset_index()\n",
    "user_merchant = user_merchant.merge(user_feaut,on=\"user_id\",how=\"left\")\n",
    "shop_feaut = shop_feaut.reset_index()\n",
    "user_merchant = user_merchant.merge(shop_feaut,right_on=\"seller_id\",left_on=\"merchant_id\" ,how=\"left\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 293,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除用户，商铺特征提取表\n",
    "del user_feaut\n",
    "del shop_feaut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  用户商铺交叉特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 294,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total\n",
       "0          2     1655         1\n",
       "1          2     7232         4\n",
       "2          2    10234         2\n",
       "3          2    15325         7\n",
       "4          2    15442         1"
      ]
     },
     "execution_count": 294,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut = user_log.groupby([\"seller_id\",\"user_id\"])[\"month\"].count()\n",
    "user_shop_feaut = user_shop_feaut.reset_index();\n",
    "user_shop_feaut.columns= [\"seller_id\",\"user_id\",\"us_total\"]\n",
    "user_shop_feaut .head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_click  us_add_car  us_buy  us_collect\n",
       "0          2     1655       1.0         NaN     NaN         NaN\n",
       "1          2     7232       3.0         NaN     1.0         NaN\n",
       "2          2    10234       1.0         NaN     NaN         1.0\n",
       "3          2    15325       6.0         NaN     1.0         NaN\n",
       "4          2    15442       1.0         NaN     NaN         NaN"
      ]
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在某商铺中各种行为次数（点击、加购、收藏和购买）\n",
    "user_shop_feaut_2 = pd.pivot_table(user_log,index=[\"seller_id\",\"user_id\"],columns=\"action_type\",values=\"day\",aggfunc=\"count\")\n",
    "user_shop_feaut_2 = user_shop_feaut_2.reset_index()\n",
    "user_shop_feaut_2.columns = [\"seller_id\",\"user_id\",\"us_click\",\"us_add_car\",\"us_buy\",\"us_collect\"]\n",
    "user_shop_feaut_2 .head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 296,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN\n",
       "1          2     7232         4       3.0         NaN     1.0         NaN\n",
       "2          2    10234         2       1.0         NaN     NaN         1.0\n",
       "3          2    15325         7       6.0         NaN     1.0         NaN\n",
       "4          2    15442         1       1.0         NaN     NaN         NaN"
      ]
     },
     "execution_count": 296,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut = user_shop_feaut.merge(user_shop_feaut_2,on=[\"seller_id\",\"user_id\"])\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 297,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_month\n",
       "0          2     1655         1\n",
       "1          2     7232         1\n",
       "2          2    10234         1\n",
       "3          2    15325         1\n",
       "4          2    15442         1"
      ]
     },
     "execution_count": 297,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在商铺中月的平均交互次数 ,各种行为平均次数（点击、加购、收藏和购买）\n",
    "user_shop_feaut_2 = user_log.groupby([\"seller_id\",\"user_id\"])[\"month\"].nunique()\n",
    "user_shop_feaut_2 = user_shop_feaut_2.reset_index()\n",
    "user_shop_feaut_2.columns =[\"seller_id\",\"user_id\",\"us_month\"]\n",
    "user_shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect  \\\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN   \n",
       "1          2     7232         4       3.0         NaN     1.0         NaN   \n",
       "2          2    10234         2       1.0         NaN     NaN         1.0   \n",
       "3          2    15325         7       6.0         NaN     1.0         NaN   \n",
       "4          2    15442         1       1.0         NaN     NaN         NaN   \n",
       "\n",
       "   us_month  \n",
       "0         1  \n",
       "1         1  \n",
       "2         1  \n",
       "3         1  \n",
       "4         1  "
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut = user_shop_feaut.merge(user_shop_feaut_2,on=[\"seller_id\",\"user_id\"])\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect  \\\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN   \n",
       "1          2     7232         4       3.0         NaN     1.0         NaN   \n",
       "2          2    10234         2       1.0         NaN     NaN         1.0   \n",
       "3          2    15325         7       6.0         NaN     1.0         NaN   \n",
       "4          2    15442         1       1.0         NaN     NaN         NaN   \n",
       "\n",
       "   us_month  us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0         1                 1.0                 1.0                  0.0   \n",
       "1         1                 4.0                 3.0                  0.0   \n",
       "2         1                 2.0                 1.0                  0.0   \n",
       "3         1                 7.0                 6.0                  0.0   \n",
       "4         1                 1.0                 1.0                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  \n",
       "0               0.0                   0.0  \n",
       "1               1.0                   0.0  \n",
       "2               0.0                   1.0  \n",
       "3               1.0                   0.0  \n",
       "4               0.0                   0.0  "
      ]
     },
     "execution_count": 299,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在商铺中月的平均交互次数 ,各种行为平均次数（点击、加购、收藏和购买）\n",
    "user_shop_feaut[\"us_month_avg_total\"] =  user_shop_feaut.us_total/user_shop_feaut.us_month\n",
    "user_shop_feaut[\"us_month_avg_click\"] = user_shop_feaut.us_click/user_shop_feaut.us_month\n",
    "user_shop_feaut[\"us_month_avg_addCar\"] =np.where(user_shop_feaut.us_add_car.isnull(),0,user_shop_feaut.us_add_car/user_shop_feaut.us_month)\n",
    "user_shop_feaut[\"us_month_avg_buy\"] =np.where(user_shop_feaut.us_buy.isnull(),0,user_shop_feaut.us_buy/user_shop_feaut.us_month)\n",
    "user_shop_feaut[\"us_month_avg_collect\"] =np.where(user_shop_feaut.us_collect.isnull(),0,user_shop_feaut.us_collect/user_shop_feaut.us_month)\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_min</th>\n",
       "      <th>us_max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1111</td>\n",
       "      <td>1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>1107</td>\n",
       "      <td>1107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>911</td>\n",
       "      <td>912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>1104</td>\n",
       "      <td>1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1111</td>\n",
       "      <td>1111</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_min  us_max\n",
       "0          2     1655    1111    1111\n",
       "1          2     7232    1107    1107\n",
       "2          2    10234     911     912\n",
       "3          2    15325    1104    1111\n",
       "4          2    15442    1111    1111"
      ]
     },
     "execution_count": 300,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut_2 = user_log.groupby([\"seller_id\",\"user_id\"])[\"time_stamp\"].agg([\"min\",\"max\"])\n",
    "user_shop_feaut_2 = user_shop_feaut_2.reset_index()\n",
    "user_shop_feaut_2.columns=[\"seller_id\",\"user_id\",\"us_min\",\"us_max\"]\n",
    "user_shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_min</th>\n",
       "      <th>us_max</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1111</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>1107</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>911</td>\n",
       "      <td>912</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>1104</td>\n",
       "      <td>1111</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1111</td>\n",
       "      <td>1111</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_min  us_max  diff\n",
       "0          2     1655    1111    1111     0\n",
       "1          2     7232    1107    1107     0\n",
       "2          2    10234     911     912     1\n",
       "3          2    15325    1104    1111     7\n",
       "4          2    15442    1111    1111     0"
      ]
     },
     "execution_count": 301,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut_2[\"diff\"] = user_shop_feaut_2.us_max-user_shop_feaut_2.us_min\n",
    "user_shop_feaut_2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect  \\\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN   \n",
       "1          2     7232         4       3.0         NaN     1.0         NaN   \n",
       "2          2    10234         2       1.0         NaN     NaN         1.0   \n",
       "3          2    15325         7       6.0         NaN     1.0         NaN   \n",
       "4          2    15442         1       1.0         NaN     NaN         NaN   \n",
       "\n",
       "   us_month  us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0         1                 1.0                 1.0                  0.0   \n",
       "1         1                 4.0                 3.0                  0.0   \n",
       "2         1                 2.0                 1.0                  0.0   \n",
       "3         1                 7.0                 6.0                  0.0   \n",
       "4         1                 1.0                 1.0                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  diff  \n",
       "0               0.0                   0.0     0  \n",
       "1               1.0                   0.0     0  \n",
       "2               0.0                   1.0     1  \n",
       "3               1.0                   0.0     7  \n",
       "4               0.0                   0.0     0  "
      ]
     },
     "execution_count": 302,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_shop_feaut_2 = user_shop_feaut_2.loc[:,[\"seller_id\",\"user_id\",\"diff\"]]\n",
    "user_shop_feaut = user_shop_feaut.merge(user_shop_feaut_2,on=[\"seller_id\",\"user_id\"])\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 303,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect  \\\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN   \n",
       "1          2     7232         4       3.0         NaN     1.0         NaN   \n",
       "2          2    10234         2       1.0         NaN     NaN         1.0   \n",
       "3          2    15325         7       6.0         NaN     1.0         NaN   \n",
       "4          2    15442         1       1.0         NaN     NaN         NaN   \n",
       "\n",
       "   us_month  us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0         1                 1.0                 1.0                  0.0   \n",
       "1         1                 4.0                 3.0                  0.0   \n",
       "2         1                 2.0                 1.0                  0.0   \n",
       "3         1                 7.0                 6.0                  0.0   \n",
       "4         1                 1.0                 1.0                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  diff  us_days  \n",
       "0               0.0                   0.0     0        1  \n",
       "1               1.0                   0.0     0        1  \n",
       "2               0.0                   1.0     1        2  \n",
       "3               1.0                   0.0     7        3  \n",
       "4               0.0                   0.0     0        1  "
      ]
     },
     "execution_count": 303,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在商铺中交互有多少天\n",
    "user_shop_feaut_2 = user_log.groupby([\"seller_id\",\"user_id\"])[\"time_stamp\"].nunique()\n",
    "user_shop_feaut_2 = user_shop_feaut_2.reset_index();\n",
    "user_shop_feaut_2.columns = [\"seller_id\",\"user_id\",\"us_days\"]\n",
    "user_shop_feaut = user_shop_feaut.merge(user_shop_feaut_2,on=[\"seller_id\",\"user_id\"])\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-101-64355fd59dfa>:2: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.\n",
      "  user_shop_feaut_2 = user_log.groupby([\"seller_id\",\"user_id\"])[\"item_id\",\"cat_id\",'brand_id'].nunique()\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "      <th>us_total</th>\n",
       "      <th>us_click</th>\n",
       "      <th>us_add_car</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>us_item_count</th>\n",
       "      <th>us_cat_count</th>\n",
       "      <th>us_brand_count</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>1655</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>7232</td>\n",
       "      <td>4</td>\n",
       "      <td>3.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>10234</td>\n",
       "      <td>2</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>15325</td>\n",
       "      <td>7</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>7.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>7</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>15442</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id  us_total  us_click  us_add_car  us_buy  us_collect  \\\n",
       "0          2     1655         1       1.0         NaN     NaN         NaN   \n",
       "1          2     7232         4       3.0         NaN     1.0         NaN   \n",
       "2          2    10234         2       1.0         NaN     NaN         1.0   \n",
       "3          2    15325         7       6.0         NaN     1.0         NaN   \n",
       "4          2    15442         1       1.0         NaN     NaN         NaN   \n",
       "\n",
       "   us_month  us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0         1                 1.0                 1.0                  0.0   \n",
       "1         1                 4.0                 3.0                  0.0   \n",
       "2         1                 2.0                 1.0                  0.0   \n",
       "3         1                 7.0                 6.0                  0.0   \n",
       "4         1                 1.0                 1.0                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  diff  us_days  us_item_count  \\\n",
       "0               0.0                   0.0     0        1              1   \n",
       "1               1.0                   0.0     0        1              3   \n",
       "2               0.0                   1.0     1        2              1   \n",
       "3               1.0                   0.0     7        3              1   \n",
       "4               0.0                   0.0     0        1              1   \n",
       "\n",
       "   us_cat_count  us_brand_count  \n",
       "0             1               1  \n",
       "1             1               1  \n",
       "2             1               1  \n",
       "3             1               1  \n",
       "4             1               1  "
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在商铺中交互的商品、商品类别和商品品牌的总个数\n",
    "user_shop_feaut_2 = user_log.groupby([\"seller_id\",\"user_id\"])[\"item_id\",\"cat_id\",'brand_id'].nunique()\n",
    "user_shop_feaut_2 = user_shop_feaut_2.reset_index()\n",
    "user_shop_feaut_2.columns=[\"seller_id\",\"user_id\",\"us_item_count\",\"us_cat_count\",\"us_brand_count\"]\n",
    "user_shop_feaut = user_shop_feaut.merge(user_shop_feaut_2,on=[\"seller_id\",\"user_id\"])\n",
    "user_shop_feaut.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {},
   "outputs": [],
   "source": [
    "del user_shop_feaut_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 305,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_buy</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>308</td>\n",
       "      <td>284.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>23</td>\n",
       "      <td>123</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>18.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>314555</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>45.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 53 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   314568         3828      0        308    284.0        NaN   12.0   \n",
       "2   314580         3853      0         21     19.0        NaN    2.0   \n",
       "3   339322         1399      0         10      9.0        NaN    1.0   \n",
       "4   314555         3828      0         54     45.0        NaN    2.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_buy  us_collect  us_month  \\\n",
       "0        NaN             5             6  ...     1.0         NaN         1   \n",
       "1       12.0            23           123  ...     1.0         1.0         1   \n",
       "2        NaN             9            10  ...     1.0         NaN         1   \n",
       "3        NaN             7             7  ...     1.0         NaN         1   \n",
       "4        7.0            14            23  ...     1.0         1.0         2   \n",
       "\n",
       "   us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0                 1.0                 NaN                  0.0   \n",
       "1                18.0                16.0                  0.0   \n",
       "2                10.0                 9.0                  0.0   \n",
       "3                 3.0                 2.0                  0.0   \n",
       "4                 4.5                 3.5                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  diff  us_days  \n",
       "0               1.0                   0.0     0        1  \n",
       "1               1.0                   1.0     1        2  \n",
       "2               1.0                   0.0     2        3  \n",
       "3               1.0                   0.0     0        1  \n",
       "4               0.5                   0.5    80        6  \n",
       "\n",
       "[5 rows x 53 columns]"
      ]
     },
     "execution_count": 305,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#合并用户-商铺特征（user_shop_feaut） 数据框到user_merchant 表内\n",
    "user_merchant = user_merchant.merge(user_shop_feaut,right_on=[\"seller_id\",\"user_id\"],left_on=[\"merchant_id\",\"user_id\"])\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 306,
   "metadata": {},
   "outputs": [],
   "source": [
    "#删除掉 用户_商铺特征表\n",
    "del user_shop_feaut"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 比值特征提取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 307,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_collect</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>308</td>\n",
       "      <td>284.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>23</td>\n",
       "      <td>123</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>18.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>314555</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>45.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>0.000035</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 54 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   314568         3828      0        308    284.0        NaN   12.0   \n",
       "2   314580         3853      0         21     19.0        NaN    2.0   \n",
       "3   339322         1399      0         10      9.0        NaN    1.0   \n",
       "4   314555         3828      0         54     45.0        NaN    2.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_collect  us_month  \\\n",
       "0        NaN             5             6  ...         NaN         1   \n",
       "1       12.0            23           123  ...         1.0         1   \n",
       "2        NaN             9            10  ...         NaN         1   \n",
       "3        NaN             7             7  ...         NaN         1   \n",
       "4        7.0            14            23  ...         1.0         2   \n",
       "\n",
       "   us_month_avg_total  us_month_avg_click  us_month_avg_addCar  \\\n",
       "0                 1.0                 NaN                  0.0   \n",
       "1                18.0                16.0                  0.0   \n",
       "2                10.0                 9.0                  0.0   \n",
       "3                 3.0                 2.0                  0.0   \n",
       "4                 4.5                 3.5                  0.0   \n",
       "\n",
       "   us_month_avg_buy  us_month_avg_collect  diff  us_days  user_total_log_p  \n",
       "0               1.0                   0.0     0        1          0.000008  \n",
       "1               1.0                   1.0     1        2          0.000201  \n",
       "2               1.0                   0.0     2        3          0.000014  \n",
       "3               1.0                   0.0     0        1          0.000007  \n",
       "4               0.5                   0.5    80        6          0.000035  \n",
       "\n",
       "[5 rows x 54 columns]"
      ]
     },
     "execution_count": 307,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户交互次数在所有用户交互总次数的占比\n",
    "all_user_log = user_log.shape[0]\n",
    "user_merchant[\"user_total_log_p\"] = user_merchant.total_log/all_user_log\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.000049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>308</td>\n",
       "      <td>284.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>23</td>\n",
       "      <td>123</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>18.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000201</td>\n",
       "      <td>0.000118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.000020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000007</td>\n",
       "      <td>0.000010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>314555</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>45.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>0.000020</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 55 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   314568         3828      0        308    284.0        NaN   12.0   \n",
       "2   314580         3853      0         21     19.0        NaN    2.0   \n",
       "3   339322         1399      0         10      9.0        NaN    1.0   \n",
       "4   314555         3828      0         54     45.0        NaN    2.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_month  us_month_avg_total  \\\n",
       "0        NaN             5             6  ...         1                 1.0   \n",
       "1       12.0            23           123  ...         1                18.0   \n",
       "2        NaN             9            10  ...         1                10.0   \n",
       "3        NaN             7             7  ...         1                 3.0   \n",
       "4        7.0            14            23  ...         2                 4.5   \n",
       "\n",
       "   us_month_avg_click  us_month_avg_addCar  us_month_avg_buy  \\\n",
       "0                 NaN                  0.0               1.0   \n",
       "1                16.0                  0.0               1.0   \n",
       "2                 9.0                  0.0               1.0   \n",
       "3                 2.0                  0.0               1.0   \n",
       "4                 3.5                  0.0               0.5   \n",
       "\n",
       "   us_month_avg_collect  diff  us_days  user_total_log_p  user_total_buy_p  \n",
       "0                   0.0     0        1          0.000008          0.000049  \n",
       "1                   1.0     1        2          0.000201          0.000118  \n",
       "2                   0.0     2        3          0.000014          0.000020  \n",
       "3                   0.0     0        1          0.000007          0.000010  \n",
       "4                   0.5    80        6          0.000035          0.000020  \n",
       "\n",
       "[5 rows x 55 columns]"
      ]
     },
     "execution_count": 308,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户购买次数在所有用户购买总次数的占比\n",
    "all_user_log_buy = (user_log.action_type==2).sum()\n",
    "user_merchant[\"user_total_buy_p\"] = user_merchant.buy_x/all_user_log_buy\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_month</th>\n",
       "      <th>us_month_avg_total</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.000049</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>308</td>\n",
       "      <td>284.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>23</td>\n",
       "      <td>123</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>18.0</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000201</td>\n",
       "      <td>0.000118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>10.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.000020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000007</td>\n",
       "      <td>0.000010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>314555</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>45.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>2</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>0.000020</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 55 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   314568         3828      0        308    284.0        NaN   12.0   \n",
       "2   314580         3853      0         21     19.0        NaN    2.0   \n",
       "3   339322         1399      0         10      9.0        NaN    1.0   \n",
       "4   314555         3828      0         54     45.0        NaN    2.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_month  us_month_avg_total  \\\n",
       "0        NaN             5             6  ...         1                 1.0   \n",
       "1       12.0            23           123  ...         1                18.0   \n",
       "2        NaN             9            10  ...         1                10.0   \n",
       "3        NaN             7             7  ...         1                 3.0   \n",
       "4        7.0            14            23  ...         2                 4.5   \n",
       "\n",
       "   us_month_avg_click  us_month_avg_addCar  us_month_avg_buy  \\\n",
       "0                 NaN                  0.0               1.0   \n",
       "1                16.0                  0.0               1.0   \n",
       "2                 9.0                  0.0               1.0   \n",
       "3                 2.0                  0.0               1.0   \n",
       "4                 3.5                  0.0               0.5   \n",
       "\n",
       "   us_month_avg_collect  diff  us_days  user_total_log_p  user_total_buy_p  \n",
       "0                   0.0     0        1          0.000008          0.000049  \n",
       "1                   1.0     1        2          0.000201          0.000118  \n",
       "2                   0.0     2        3          0.000014          0.000020  \n",
       "3                   0.0     0        1          0.000007          0.000010  \n",
       "4                   0.5    80        6          0.000035          0.000020  \n",
       "\n",
       "[5 rows x 55 columns]"
      ]
     },
     "execution_count": 309,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户交互次数在所有用户交互总次数的占比\n",
    "all_user_log = user_log.shape[0]\n",
    "user_merchant[\"user_total_log_p\"] = user_merchant.total_log/all_user_log\n",
    "#用户购买次数在所有用户购买总次数的占比\n",
    "all_user_log_buy = (user_log.action_type==2).sum()\n",
    "user_merchant[\"user_total_buy_p\"] = user_merchant.buy_x/all_user_log_buy\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_month_avg_click</th>\n",
       "      <th>us_month_avg_addCar</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "      <th>shop_total_log_p</th>\n",
       "      <th>shop_total_buy_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.003010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>314568</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>308</td>\n",
       "      <td>284.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12.0</td>\n",
       "      <td>12.0</td>\n",
       "      <td>23</td>\n",
       "      <td>123</td>\n",
       "      <td>...</td>\n",
       "      <td>16.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0.000201</td>\n",
       "      <td>0.000118</td>\n",
       "      <td>0.050034</td>\n",
       "      <td>0.022763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>314580</td>\n",
       "      <td>3853</td>\n",
       "      <td>0</td>\n",
       "      <td>21</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>9.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000014</td>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.001956</td>\n",
       "      <td>0.000984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>339322</td>\n",
       "      <td>1399</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>9.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000007</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000787</td>\n",
       "      <td>0.000708</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>314555</td>\n",
       "      <td>3828</td>\n",
       "      <td>0</td>\n",
       "      <td>54</td>\n",
       "      <td>45.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2.0</td>\n",
       "      <td>7.0</td>\n",
       "      <td>14</td>\n",
       "      <td>23</td>\n",
       "      <td>...</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>80</td>\n",
       "      <td>6</td>\n",
       "      <td>0.000035</td>\n",
       "      <td>0.000020</td>\n",
       "      <td>0.050034</td>\n",
       "      <td>0.022763</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 57 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   314568         3828      0        308    284.0        NaN   12.0   \n",
       "2   314580         3853      0         21     19.0        NaN    2.0   \n",
       "3   339322         1399      0         10      9.0        NaN    1.0   \n",
       "4   314555         3828      0         54     45.0        NaN    2.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_month_avg_click  \\\n",
       "0        NaN             5             6  ...                 NaN   \n",
       "1       12.0            23           123  ...                16.0   \n",
       "2        NaN             9            10  ...                 9.0   \n",
       "3        NaN             7             7  ...                 2.0   \n",
       "4        7.0            14            23  ...                 3.5   \n",
       "\n",
       "   us_month_avg_addCar  us_month_avg_buy  us_month_avg_collect  diff  us_days  \\\n",
       "0                  0.0               1.0                   0.0     0        1   \n",
       "1                  0.0               1.0                   1.0     1        2   \n",
       "2                  0.0               1.0                   0.0     2        3   \n",
       "3                  0.0               1.0                   0.0     0        1   \n",
       "4                  0.0               0.5                   0.5    80        6   \n",
       "\n",
       "   user_total_log_p  user_total_buy_p  shop_total_log_p  shop_total_buy_p  \n",
       "0          0.000008          0.000049          0.002593          0.003010  \n",
       "1          0.000201          0.000118          0.050034          0.022763  \n",
       "2          0.000014          0.000020          0.001956          0.000984  \n",
       "3          0.000007          0.000010          0.000787          0.000708  \n",
       "4          0.000035          0.000020          0.050034          0.022763  \n",
       "\n",
       "[5 rows x 57 columns]"
      ]
     },
     "execution_count": 310,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺中交互次数在所有商铺交互次数的占比\n",
    "user_merchant[\"shop_total_log_p\"] = user_merchant.total_count/all_user_log\n",
    "#商铺中交购买次数在所有商铺购买总次数的占比\n",
    "user_merchant[\"shop_total_buy_p\"] = user_merchant.buy_y/all_user_log_buy\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>user_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  user_id\n",
       "0          2       10\n",
       "1          8       11\n",
       "2          9        8\n",
       "3         10       44\n",
       "4         13       31"
      ]
     },
     "execution_count": 312,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_featu =  user_log.loc[user_log.action_type==2].groupby(\"seller_id\")[\"user_id\"].nunique()\n",
    "shop_featu = shop_featu.reset_index()\n",
    "shop_featu.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>9</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>13</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  Shop_buy_users\n",
       "0          2              10\n",
       "1          8              11\n",
       "2          9               8\n",
       "3         10              44\n",
       "4         13              31"
      ]
     },
     "execution_count": 313,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shop_featu.columns = [\"seller_id\",\"Shop_buy_users\"]\n",
    "shop_featu.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_month_avg_buy</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "      <th>shop_total_log_p</th>\n",
       "      <th>shop_total_buy_p</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000015</td>\n",
       "      <td>0.000030</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000059</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>1.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000055</td>\n",
       "      <td>0.000039</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 59 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_month_avg_buy  \\\n",
       "0        NaN             5             6  ...               1.0   \n",
       "1        4.0            10            24  ...               2.0   \n",
       "2        1.0             6            10  ...               1.0   \n",
       "3        NaN            14            24  ...               1.0   \n",
       "4        NaN            17            28  ...               1.0   \n",
       "\n",
       "   us_month_avg_collect  diff  us_days  user_total_log_p  user_total_buy_p  \\\n",
       "0                   0.0     0        1          0.000008          0.000049   \n",
       "1                   3.0     4        3          0.000033          0.000049   \n",
       "2                   0.0     0        1          0.000015          0.000030   \n",
       "3                   0.0     0        1          0.000023          0.000059   \n",
       "4                   0.0     0        1          0.000055          0.000039   \n",
       "\n",
       "   shop_total_log_p  shop_total_buy_p  seller_id  Shop_buy_users  \n",
       "0          0.002593           0.00301       3609             126  \n",
       "1          0.002593           0.00301       3609             126  \n",
       "2          0.002593           0.00301       3609             126  \n",
       "3          0.002593           0.00301       3609             126  \n",
       "4          0.002593           0.00301       3609             126  \n",
       "\n",
       "[5 rows x 59 columns]"
      ]
     },
     "execution_count": 314,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant = user_merchant.merge(shop_featu,left_on=\"seller_id_y\",right_on=\"seller_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_month_avg_collect</th>\n",
       "      <th>diff</th>\n",
       "      <th>us_days</th>\n",
       "      <th>user_total_log_p</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "      <th>shop_total_log_p</th>\n",
       "      <th>shop_total_buy_p</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000008</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>0.000033</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000015</td>\n",
       "      <td>0.000030</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000023</td>\n",
       "      <td>0.000059</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.000055</td>\n",
       "      <td>0.000039</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 60 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_month_avg_collect  diff  \\\n",
       "0        NaN             5             6  ...                   0.0     0   \n",
       "1        4.0            10            24  ...                   3.0     4   \n",
       "2        1.0             6            10  ...                   0.0     0   \n",
       "3        NaN            14            24  ...                   0.0     0   \n",
       "4        NaN            17            28  ...                   0.0     0   \n",
       "\n",
       "   us_days  user_total_log_p  user_total_buy_p  shop_total_log_p  \\\n",
       "0        1          0.000008          0.000049          0.002593   \n",
       "1        3          0.000033          0.000049          0.002593   \n",
       "2        1          0.000015          0.000030          0.002593   \n",
       "3        1          0.000023          0.000059          0.002593   \n",
       "4        1          0.000055          0.000039          0.002593   \n",
       "\n",
       "   shop_total_buy_p  seller_id  Shop_buy_users  shop_buyuser_totalbuyuser_p  \n",
       "0           0.00301       3609             126                     0.005451  \n",
       "1           0.00301       3609             126                     0.005451  \n",
       "2           0.00301       3609             126                     0.005451  \n",
       "3           0.00301       3609             126                     0.005451  \n",
       "4           0.00301       3609             126                     0.005451  \n",
       "\n",
       "[5 rows x 60 columns]"
      ]
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#商铺中购买的用户数在所有商铺购买用户数的占比\n",
    "all_buy_user_count =user_log.user_id[user_log.action_type==2].nunique()\n",
    "user_merchant[\"shop_buyuser_totalbuyuser_p\"] = user_merchant.Shop_buy_users/all_buy_user_count\n",
    "user_merchant.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>user_total_buy_p</th>\n",
       "      <th>shop_total_log_p</th>\n",
       "      <th>shop_total_buy_p</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000049</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000030</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000059</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000039</td>\n",
       "      <td>0.002593</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.129412</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.002769</td>\n",
       "      <td>0.003268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 64 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  user_total_buy_p  \\\n",
       "0        NaN             5             6  ...          0.000049   \n",
       "1        4.0            10            24  ...          0.000049   \n",
       "2        1.0             6            10  ...          0.000030   \n",
       "3        NaN            14            24  ...          0.000059   \n",
       "4        NaN            17            28  ...          0.000039   \n",
       "\n",
       "   shop_total_log_p  shop_total_buy_p  seller_id  Shop_buy_users  \\\n",
       "0          0.002593           0.00301       3609             126   \n",
       "1          0.002593           0.00301       3609             126   \n",
       "2          0.002593           0.00301       3609             126   \n",
       "3          0.002593           0.00301       3609             126   \n",
       "4          0.002593           0.00301       3609             126   \n",
       "\n",
       "   shop_buyuser_totalbuyuser_p  us_user_log_p  us_user_buy_p  us_shop_log_p  \\\n",
       "0                     0.005451       0.083333       0.200000       0.000252   \n",
       "1                     0.005451       0.490196       0.400000       0.006292   \n",
       "2                     0.005451       0.130435       0.333333       0.000755   \n",
       "3                     0.005451       0.055556       0.166667       0.000503   \n",
       "4                     0.005451       0.129412       0.250000       0.002769   \n",
       "\n",
       "   us_shop_buy_p  \n",
       "0       0.003268  \n",
       "1       0.006536  \n",
       "2       0.003268  \n",
       "3       0.003268  \n",
       "4       0.003268  \n",
       "\n",
       "[5 rows x 64 columns]"
      ]
     },
     "execution_count": 317,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用户在某商铺的交互次数在该用户所有交互次数的占比\n",
    "user_merchant[\"us_user_log_p\"] = user_merchant.us_total/user_merchant.total_log\n",
    "#用户在某商铺的购买次数在该用户所有购买次数的占比\n",
    "user_merchant[\"us_user_buy_p\"] = user_merchant.us_buy/user_merchant.buy_x\n",
    "#用户在某商铺的交互次数在该商铺所有交互次数中的占比\n",
    "user_merchant[\"us_shop_log_p\"] = user_merchant.us_total/user_merchant.total_count\n",
    "#用户在某商铺的购买次数在该商铺所有购买次数中的占比\n",
    "user_merchant[\"us_shop_buy_p\"] = user_merchant.us_buy/user_merchant.buy_y\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>uid_day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26516_1107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26516_1107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>26516</td>\n",
       "      <td>416965</td>\n",
       "      <td>1401</td>\n",
       "      <td>586</td>\n",
       "      <td>5579.0</td>\n",
       "      <td>1107</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26516_1107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>26516</td>\n",
       "      <td>352345</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26516_1101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>26516</td>\n",
       "      <td>142231</td>\n",
       "      <td>177</td>\n",
       "      <td>2565</td>\n",
       "      <td>8149.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>26516_1101</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  item_id  cat_id  seller_id  brand_id  time_stamp  action_type  \\\n",
       "0    26516   416965    1401        586    5579.0        1107            0   \n",
       "1    26516   416965    1401        586    5579.0        1107            0   \n",
       "2    26516   416965    1401        586    5579.0        1107            0   \n",
       "3    26516   352345     177       2565    8149.0        1101            0   \n",
       "4    26516   142231     177       2565    8149.0        1101            0   \n",
       "\n",
       "   month  day  age_range  gender     uid_day  \n",
       "0     11    7        4.0     1.0  26516_1107  \n",
       "1     11    7        4.0     1.0  26516_1107  \n",
       "2     11    7        4.0     1.0  26516_1107  \n",
       "3     11    1        4.0     1.0  26516_1101  \n",
       "4     11    1        4.0     1.0  26516_1101  "
      ]
     },
     "execution_count": 318,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log[\"uid_day\"] = user_log.user_id.astype(\"str\") +\"_\"+ user_log.time_stamp.astype(\"str\")\n",
    "user_log.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [],
   "source": [
    "#商铺中回购（购买次数>1）的用户数占总回购用户数的比例\n",
    "#认为一天的为一个订单\n",
    "#总回购用户数\n",
    "temp = user_log.uid_day[user_log.action_type==2].unique()\n",
    "buy_double_user = (pd.Series(map(lambda x:x.split(\"_\")[0],temp)).value_counts()>1).sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义函数获得指定店铺的回归用户数\n",
    "def get_shop_double(shop_id):\n",
    "    temp = user_log.uid_day[(user_log.action_type==2) & (user_log.seller_id == shop_id)].unique()\n",
    "    buy_double_user = (pd.Series(map(lambda x:x.split(\"_\")[0],temp)).value_counts()>1).sum()\n",
    "    return [shop_id,buy_double_user]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[586, 9]"
      ]
     },
     "execution_count": 320,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_shop_double(586)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 322,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>seller_id</th>\n",
       "      <th>double_user</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>586</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2565</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1892</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>862</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2403</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   seller_id  double_user\n",
       "0        586            9\n",
       "1       2565            6\n",
       "2       1892           15\n",
       "3        862            0\n",
       "4       2403            1"
      ]
     },
     "execution_count": 322,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#遍历每个店铺获得其回购用户数\n",
    "temp = pd.DataFrame(map(get_shop_double,user_log.seller_id.unique()),columns=[\"seller_id\",\"double_user\"])\n",
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>shop_total_buy_p</th>\n",
       "      <th>seller_id_x</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>seller_id_y</th>\n",
       "      <th>double_user</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>0.00301</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.129412</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.002769</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 66 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  shop_total_buy_p  seller_id_x  \\\n",
       "0        NaN             5             6  ...           0.00301         3609   \n",
       "1        4.0            10            24  ...           0.00301         3609   \n",
       "2        1.0             6            10  ...           0.00301         3609   \n",
       "3        NaN            14            24  ...           0.00301         3609   \n",
       "4        NaN            17            28  ...           0.00301         3609   \n",
       "\n",
       "   Shop_buy_users  shop_buyuser_totalbuyuser_p  us_user_log_p  us_user_buy_p  \\\n",
       "0             126                     0.005451       0.083333       0.200000   \n",
       "1             126                     0.005451       0.490196       0.400000   \n",
       "2             126                     0.005451       0.130435       0.333333   \n",
       "3             126                     0.005451       0.055556       0.166667   \n",
       "4             126                     0.005451       0.129412       0.250000   \n",
       "\n",
       "   us_shop_log_p  us_shop_buy_p  seller_id_y  double_user  \n",
       "0       0.000252       0.003268         3609            8  \n",
       "1       0.006292       0.006536         3609            8  \n",
       "2       0.000755       0.003268         3609            8  \n",
       "3       0.000503       0.003268         3609            8  \n",
       "4       0.002769       0.003268         3609            8  \n",
       "\n",
       "[5 rows x 66 columns]"
      ]
     },
     "execution_count": 323,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant = user_merchant.merge(temp,right_on=\"seller_id\",left_on=\"merchant_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>cat_count_x</th>\n",
       "      <th>brand_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>double_user</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "      <th>shop_Repurchase_cat_P</th>\n",
       "      <th>User_RepurchaseShop_P</th>\n",
       "      <th>User_Repurchase_P</th>\n",
       "      <th>shop_doubleU_totaldoubleU_p</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.000565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>...</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.003436</td>\n",
       "      <td>0.027778</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.884615</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>12</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.000565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.000565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "      <td>...</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.000565</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 65 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  total_log  click_x  add_car_x  buy_x  collect_x  seller_count  \\\n",
       "0      0         12      7.0        0.0    5.0        0.0             5   \n",
       "1      1         12      7.0        0.0    5.0        0.0             5   \n",
       "2      0         51     42.0        0.0    5.0        4.0            10   \n",
       "3      0         23     19.0        0.0    3.0        1.0             6   \n",
       "4      0         36     30.0        0.0    6.0        0.0            14   \n",
       "\n",
       "   item_count_x  cat_count_x  brand_count_x  ...  us_user_log_p  \\\n",
       "0             6            5              5  ...       0.083333   \n",
       "1             6            5              5  ...       0.083333   \n",
       "2            24           12             10  ...       0.490196   \n",
       "3            10            5              6  ...       0.130435   \n",
       "4            24           15             15  ...       0.055556   \n",
       "\n",
       "   us_user_buy_p  us_shop_log_p  us_shop_buy_p  double_user  \\\n",
       "0       0.200000       0.000252       0.003268            8   \n",
       "1       0.200000       0.003436       0.027778            0   \n",
       "2       0.400000       0.006292       0.006536            8   \n",
       "3       0.333333       0.000755       0.003268            8   \n",
       "4       0.166667       0.000503       0.003268            8   \n",
       "\n",
       "   shop_Repurchase_P  shop_Repurchase_cat_P  User_RepurchaseShop_P  \\\n",
       "0                0.1               0.986111               0.000000   \n",
       "1                0.0               0.884615               0.000000   \n",
       "2                0.1               0.986111               0.666667   \n",
       "3                0.1               0.986111               0.500000   \n",
       "4                0.1               0.986111               0.200000   \n",
       "\n",
       "   User_Repurchase_P  shop_doubleU_totaldoubleU_p  \n",
       "0           0.666667                     0.000565  \n",
       "1           0.666667                     0.000000  \n",
       "2           0.666667                     0.000565  \n",
       "3           0.500000                     0.000565  \n",
       "4           0.500000                     0.000565  \n",
       "\n",
       "[5 rows x 65 columns]"
      ]
     },
     "execution_count": 338,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算比值\n",
    "user_merchant[\"shop_doubleU_totaldoubleU_p\"] = user_merchant.double_user/buy_double_user\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#商铺中回购的总次数占商铺中用户总购买次数的比例\n",
    "#获得指定店铺的回购用户比\n",
    "def get_shop_double_order(shop_id):\n",
    "    temp = user_log.uid_day[(user_log.action_type==2) & (user_log.seller_id == shop_id)].unique()\n",
    "    temp = pd.Series(map(lambda x:x.split(\"_\")[0],temp)).value_counts()\n",
    "    total_buy = temp.sum()\n",
    "    double = (temp-1).sum()\n",
    "    return [shop_id,double/total_buy]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[586, 0.078125]"
      ]
     },
     "execution_count": 325,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_shop_double_order(586)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 326,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>586</td>\n",
       "      <td>0.078125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2565</td>\n",
       "      <td>0.047059</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1892</td>\n",
       "      <td>0.060052</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>862</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2403</td>\n",
       "      <td>0.013158</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   merchant_id  shop_Repurchase_P\n",
       "0          586           0.078125\n",
       "1         2565           0.047059\n",
       "2         1892           0.060052\n",
       "3          862           0.000000\n",
       "4         2403           0.013158"
      ]
     },
     "execution_count": 326,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#遍历每一个店铺，获得其商铺中回购的总次数占商铺中用户总购买次数的比例\n",
    "temp = pd.DataFrame(map(get_shop_double_order,user_log.seller_id.unique()),columns=[\"merchant_id\",\"shop_Repurchase_P\"])\n",
    "temp.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>seller_id_x</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>seller_id_y</th>\n",
       "      <th>double_user</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>3609</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.129412</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.002769</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 67 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  seller_id_x  Shop_buy_users  \\\n",
       "0        NaN             5             6  ...         3609             126   \n",
       "1        4.0            10            24  ...         3609             126   \n",
       "2        1.0             6            10  ...         3609             126   \n",
       "3        NaN            14            24  ...         3609             126   \n",
       "4        NaN            17            28  ...         3609             126   \n",
       "\n",
       "   shop_buyuser_totalbuyuser_p  us_user_log_p  us_user_buy_p  us_shop_log_p  \\\n",
       "0                     0.005451       0.083333       0.200000       0.000252   \n",
       "1                     0.005451       0.490196       0.400000       0.006292   \n",
       "2                     0.005451       0.130435       0.333333       0.000755   \n",
       "3                     0.005451       0.055556       0.166667       0.000503   \n",
       "4                     0.005451       0.129412       0.250000       0.002769   \n",
       "\n",
       "   us_shop_buy_p  seller_id_y  double_user  shop_Repurchase_P  \n",
       "0       0.003268         3609            8                0.1  \n",
       "1       0.006536         3609            8                0.1  \n",
       "2       0.003268         3609            8                0.1  \n",
       "3       0.003268         3609            8                0.1  \n",
       "4       0.003268         3609            8                0.1  \n",
       "\n",
       "[5 rows x 67 columns]"
      ]
     },
     "execution_count": 327,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_merchant = user_merchant.merge(temp,on=\"merchant_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 328,
   "metadata": {},
   "outputs": [],
   "source": [
    "#商铺中回购的商品类别总数在商铺中用户购买商品类别总数的占比\n",
    "def get_shop_double_cat(shop_id):\n",
    "    temp = user_log.cat_id[(user_log.action_type==2) & (user_log.seller_id == shop_id)].value_counts()\n",
    "    zong = temp.sum()\n",
    "    fugou = (temp-1).sum()\n",
    "    return [shop_id,fugou/zong]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 329,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>Shop_buy_users</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>seller_id_y</th>\n",
       "      <th>double_user</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "      <th>shop_Repurchase_cat_P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>126392</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>85</td>\n",
       "      <td>81.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>17</td>\n",
       "      <td>28</td>\n",
       "      <td>...</td>\n",
       "      <td>126</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.129412</td>\n",
       "      <td>0.250000</td>\n",
       "      <td>0.002769</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 68 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   395590         3609      0         51     42.0        NaN    5.0   \n",
       "2   395570         3609      0         23     19.0        NaN    3.0   \n",
       "3   250943         3609      0         36     30.0        NaN    6.0   \n",
       "4   126392         3609      0         85     81.0        NaN    4.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  Shop_buy_users  \\\n",
       "0        NaN             5             6  ...             126   \n",
       "1        4.0            10            24  ...             126   \n",
       "2        1.0             6            10  ...             126   \n",
       "3        NaN            14            24  ...             126   \n",
       "4        NaN            17            28  ...             126   \n",
       "\n",
       "   shop_buyuser_totalbuyuser_p  us_user_log_p  us_user_buy_p  us_shop_log_p  \\\n",
       "0                     0.005451       0.083333       0.200000       0.000252   \n",
       "1                     0.005451       0.490196       0.400000       0.006292   \n",
       "2                     0.005451       0.130435       0.333333       0.000755   \n",
       "3                     0.005451       0.055556       0.166667       0.000503   \n",
       "4                     0.005451       0.129412       0.250000       0.002769   \n",
       "\n",
       "   us_shop_buy_p  seller_id_y  double_user  shop_Repurchase_P  \\\n",
       "0       0.003268         3609            8                0.1   \n",
       "1       0.006536         3609            8                0.1   \n",
       "2       0.003268         3609            8                0.1   \n",
       "3       0.003268         3609            8                0.1   \n",
       "4       0.003268         3609            8                0.1   \n",
       "\n",
       "   shop_Repurchase_cat_P  \n",
       "0               0.986111  \n",
       "1               0.986111  \n",
       "2               0.986111  \n",
       "3               0.986111  \n",
       "4               0.986111  \n",
       "\n",
       "[5 rows x 68 columns]"
      ]
     },
     "execution_count": 329,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#遍历所有商铺，获得其品类回购率\n",
    "temp =pd.DataFrame(list(map(get_shop_double_cat,user_log.seller_id.unique())),columns=[\"merchant_id\",\"shop_Repurchase_cat_P\"])\n",
    "user_merchant = user_merchant.merge(temp,on=\"merchant_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 330,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用户有回购的商铺数在该用户所有购买行为商铺数的占比\n",
    "def get_shop_double_us(user_id):\n",
    "    temp = user_log.seller_id[(user_log.action_type==2) & (user_log.user_id == user_id)].value_counts()\n",
    "    zong = temp.size\n",
    "    fugou = (temp>1).sum()\n",
    "    return [user_id,fugou/zong]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 331,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>shop_buyuser_totalbuyuser_p</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>seller_id_y</th>\n",
       "      <th>double_user</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "      <th>shop_Repurchase_cat_P</th>\n",
       "      <th>User_RepurchaseShop_P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>251021</td>\n",
       "      <td>3622</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000952</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.003436</td>\n",
       "      <td>0.027778</td>\n",
       "      <td>3622</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.884615</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.005451</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.200000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 69 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   251021         3622      1         12      7.0        NaN    5.0   \n",
       "2   395590         3609      0         51     42.0        NaN    5.0   \n",
       "3   395570         3609      0         23     19.0        NaN    3.0   \n",
       "4   250943         3609      0         36     30.0        NaN    6.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  shop_buyuser_totalbuyuser_p  \\\n",
       "0        NaN             5             6  ...                     0.005451   \n",
       "1        NaN             5             6  ...                     0.000952   \n",
       "2        4.0            10            24  ...                     0.005451   \n",
       "3        1.0             6            10  ...                     0.005451   \n",
       "4        NaN            14            24  ...                     0.005451   \n",
       "\n",
       "   us_user_log_p  us_user_buy_p  us_shop_log_p  us_shop_buy_p  seller_id_y  \\\n",
       "0       0.083333       0.200000       0.000252       0.003268         3609   \n",
       "1       0.083333       0.200000       0.003436       0.027778         3622   \n",
       "2       0.490196       0.400000       0.006292       0.006536         3609   \n",
       "3       0.130435       0.333333       0.000755       0.003268         3609   \n",
       "4       0.055556       0.166667       0.000503       0.003268         3609   \n",
       "\n",
       "   double_user  shop_Repurchase_P  shop_Repurchase_cat_P  \\\n",
       "0            8                0.1               0.986111   \n",
       "1            0                0.0               0.884615   \n",
       "2            8                0.1               0.986111   \n",
       "3            8                0.1               0.986111   \n",
       "4            8                0.1               0.986111   \n",
       "\n",
       "   User_RepurchaseShop_P  \n",
       "0               0.000000  \n",
       "1               0.000000  \n",
       "2               0.666667  \n",
       "3               0.500000  \n",
       "4               0.200000  \n",
       "\n",
       "[5 rows x 69 columns]"
      ]
     },
     "execution_count": 331,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#遍历所有用户 ，获得其有回购的商铺数在该用户所有购买行为商铺数的占比\n",
    "temp =pd.DataFrame(list(map(get_shop_double_us,user_log.user_id.unique())),columns=[\"user_id\",\"User_RepurchaseShop_P\"])\n",
    "user_merchant = user_merchant.merge(temp,on=\"user_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 332,
   "metadata": {},
   "outputs": [],
   "source": [
    "#用户回购次数在该用户所有购买总次数的占比\n",
    "def get_User_Repurchase(user_id):\n",
    "    temp = user_log.uid_day[(user_log.action_type==2) & (user_log.user_id == user_id)].unique()\n",
    "    temp = pd.Series(map(lambda x:x.split(\"_\")[0],temp)).value_counts()\n",
    "    total_buy = temp.sum()\n",
    "    double = (temp-1).sum()\n",
    "    return [user_id,double/total_buy]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[251021, 0.6666666666666666]"
      ]
     },
     "execution_count": 333,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_User_Repurchase(251021)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item_id</th>\n",
       "      <th>cat_id</th>\n",
       "      <th>seller_id</th>\n",
       "      <th>brand_id</th>\n",
       "      <th>time_stamp</th>\n",
       "      <th>action_type</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>age_range</th>\n",
       "      <th>gender</th>\n",
       "      <th>uid_day</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1280746</th>\n",
       "      <td>251021</td>\n",
       "      <td>613760</td>\n",
       "      <td>1169</td>\n",
       "      <td>3622</td>\n",
       "      <td>6293.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>251021_1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1280747</th>\n",
       "      <td>251021</td>\n",
       "      <td>784167</td>\n",
       "      <td>1553</td>\n",
       "      <td>3828</td>\n",
       "      <td>1446.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>251021_1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1280748</th>\n",
       "      <td>251021</td>\n",
       "      <td>18724</td>\n",
       "      <td>177</td>\n",
       "      <td>3609</td>\n",
       "      <td>6826.0</td>\n",
       "      <td>1111</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>11</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>251021_1111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1280753</th>\n",
       "      <td>251021</td>\n",
       "      <td>145684</td>\n",
       "      <td>1505</td>\n",
       "      <td>3160</td>\n",
       "      <td>4065.0</td>\n",
       "      <td>1030</td>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>251021_1030</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1280756</th>\n",
       "      <td>251021</td>\n",
       "      <td>316577</td>\n",
       "      <td>639</td>\n",
       "      <td>1000</td>\n",
       "      <td>1954.0</td>\n",
       "      <td>1101</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>251021_1101</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         user_id  item_id  cat_id  seller_id  brand_id  time_stamp  \\\n",
       "1280746   251021   613760    1169       3622    6293.0        1111   \n",
       "1280747   251021   784167    1553       3828    1446.0        1111   \n",
       "1280748   251021    18724     177       3609    6826.0        1111   \n",
       "1280753   251021   145684    1505       3160    4065.0        1030   \n",
       "1280756   251021   316577     639       1000    1954.0        1101   \n",
       "\n",
       "         action_type  month  day  age_range  gender      uid_day  \n",
       "1280746            2     11   11        5.0     0.0  251021_1111  \n",
       "1280747            2     11   11        5.0     0.0  251021_1111  \n",
       "1280748            2     11   11        5.0     0.0  251021_1111  \n",
       "1280753            2     10   30        5.0     0.0  251021_1030  \n",
       "1280756            2     11    1        5.0     0.0  251021_1101  "
      ]
     },
     "execution_count": 334,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_log.loc[(user_log.action_type==2) & (user_log.user_id == 251021),:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>merchant_id</th>\n",
       "      <th>label</th>\n",
       "      <th>total_log</th>\n",
       "      <th>click_x</th>\n",
       "      <th>add_car_x</th>\n",
       "      <th>buy_x</th>\n",
       "      <th>collect_x</th>\n",
       "      <th>seller_count</th>\n",
       "      <th>item_count_x</th>\n",
       "      <th>...</th>\n",
       "      <th>us_user_log_p</th>\n",
       "      <th>us_user_buy_p</th>\n",
       "      <th>us_shop_log_p</th>\n",
       "      <th>us_shop_buy_p</th>\n",
       "      <th>seller_id_y</th>\n",
       "      <th>double_user</th>\n",
       "      <th>shop_Repurchase_P</th>\n",
       "      <th>shop_Repurchase_cat_P</th>\n",
       "      <th>User_RepurchaseShop_P</th>\n",
       "      <th>User_Repurchase_P</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>251021</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.000252</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>251021</td>\n",
       "      <td>3622</td>\n",
       "      <td>1</td>\n",
       "      <td>12</td>\n",
       "      <td>7.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>...</td>\n",
       "      <td>0.083333</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.003436</td>\n",
       "      <td>0.027778</td>\n",
       "      <td>3622</td>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.884615</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>395590</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>51</td>\n",
       "      <td>42.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>10</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.490196</td>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.006292</td>\n",
       "      <td>0.006536</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.666667</td>\n",
       "      <td>0.666667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>395570</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>19.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>...</td>\n",
       "      <td>0.130435</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.000755</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>250943</td>\n",
       "      <td>3609</td>\n",
       "      <td>0</td>\n",
       "      <td>36</td>\n",
       "      <td>30.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>14</td>\n",
       "      <td>24</td>\n",
       "      <td>...</td>\n",
       "      <td>0.055556</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.000503</td>\n",
       "      <td>0.003268</td>\n",
       "      <td>3609</td>\n",
       "      <td>8</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.986111</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 70 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   user_id  merchant_id  label  total_log  click_x  add_car_x  buy_x  \\\n",
       "0   251021         3609      0         12      7.0        NaN    5.0   \n",
       "1   251021         3622      1         12      7.0        NaN    5.0   \n",
       "2   395590         3609      0         51     42.0        NaN    5.0   \n",
       "3   395570         3609      0         23     19.0        NaN    3.0   \n",
       "4   250943         3609      0         36     30.0        NaN    6.0   \n",
       "\n",
       "   collect_x  seller_count  item_count_x  ...  us_user_log_p  us_user_buy_p  \\\n",
       "0        NaN             5             6  ...       0.083333       0.200000   \n",
       "1        NaN             5             6  ...       0.083333       0.200000   \n",
       "2        4.0            10            24  ...       0.490196       0.400000   \n",
       "3        1.0             6            10  ...       0.130435       0.333333   \n",
       "4        NaN            14            24  ...       0.055556       0.166667   \n",
       "\n",
       "   us_shop_log_p  us_shop_buy_p  seller_id_y  double_user  shop_Repurchase_P  \\\n",
       "0       0.000252       0.003268         3609            8                0.1   \n",
       "1       0.003436       0.027778         3622            0                0.0   \n",
       "2       0.006292       0.006536         3609            8                0.1   \n",
       "3       0.000755       0.003268         3609            8                0.1   \n",
       "4       0.000503       0.003268         3609            8                0.1   \n",
       "\n",
       "   shop_Repurchase_cat_P  User_RepurchaseShop_P  User_Repurchase_P  \n",
       "0               0.986111               0.000000           0.666667  \n",
       "1               0.884615               0.000000           0.666667  \n",
       "2               0.986111               0.666667           0.666667  \n",
       "3               0.986111               0.500000           0.500000  \n",
       "4               0.986111               0.200000           0.500000  \n",
       "\n",
       "[5 rows x 70 columns]"
      ]
     },
     "execution_count": 335,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#遍历所有用户 ，获得其回购次数在该用户所有购买总次数的占比\n",
    "temp =pd.DataFrame(list(map(get_User_Repurchase,user_log.user_id.unique())),columns=[\"user_id\",\"User_Repurchase_P\"])\n",
    "user_merchant = user_merchant.merge(temp,on=\"user_id\")\n",
    "user_merchant.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['label', 'total_log', 'click_x', 'add_car_x', 'buy_x', 'collect_x',\n",
       "       'seller_count', 'item_count_x', 'cat_count_x', 'brand_count_x',\n",
       "       'month_count', 'day_count', 'month_avg_log', 'month_avg_buy',\n",
       "       'day_avg_log', 'day_avg_buy', 'total_count', 'click_y', 'add_car_y',\n",
       "       'buy_y', 'collect_y', 'user_count', 'item_count_y', 'cat_count_y',\n",
       "       'brand_count_y', 'month', 'month_avg_user', 'less18', 'between18and24',\n",
       "       'between25and29', 'between30and34', 'between35and39', 'between40and49',\n",
       "       'grate50', 'gender_F', 'gender_M', 'us_total', 'us_click', 'us_add_car',\n",
       "       'us_buy', 'us_collect', 'us_month', 'us_month_avg_total',\n",
       "       'us_month_avg_click', 'us_month_avg_addCar', 'us_month_avg_buy',\n",
       "       'us_month_avg_collect', 'diff', 'us_days', 'user_total_log_p',\n",
       "       'user_total_buy_p', 'shop_total_log_p', 'shop_total_buy_p',\n",
       "       'Shop_buy_users', 'shop_buyuser_totalbuyuser_p', 'us_user_log_p',\n",
       "       'us_user_buy_p', 'us_shop_log_p', 'us_shop_buy_p', 'double_user',\n",
       "       'shop_Repurchase_P', 'shop_Repurchase_cat_P', 'User_RepurchaseShop_P',\n",
       "       'User_Repurchase_P'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 337,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#缺失值设置为0\n",
    "user_merchant.fillna(0,inplace=True)\n",
    "#删除不需要的列\n",
    "user_merchant = user_merchant.drop([\"seller_id_x\",\"seller_id_y\",\"user_id\",\"merchant_id\"],axis=1)\n",
    "user_merchant.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 建模及评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import time\n",
    "# 模型处理模块\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "# 标准化处理模块\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "# 常规模型\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 集成学习和stacking模型\n",
    "from sklearn.ensemble import AdaBoostClassifier, GradientBoostingClassifier, RandomForestClassifier\n",
    "import xgboost as xgb\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "from mlxtend.classifier import StackingClassifier\n",
    "# 评价标准模块\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score,roc_auc_score,recall_score,precision_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 340,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train,X_test,y_train,y_test = train_test_split(user_merchant.iloc[:,1:],user_merchant.label,test_size=0.3,random_state=6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 341,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train_model(X_train, y_train, X_test, y_test,\n",
    "               model,model_name):\n",
    "    \n",
    "    print('训练{}'.format(model_name))\n",
    "    \n",
    "    clf=model\n",
    "    start = time.time()\n",
    "    clf.fit(X_train, y_train.values.ravel())\n",
    "    \n",
    "     #验证模型\n",
    "    print('训练准确率：{:.4f}'.format(clf.score(X_train, y_train)))\n",
    "    \n",
    "    \n",
    "    predict=clf.predict(X_test)\n",
    "    score = clf.score(X_test, y_test)\n",
    "    precision=precision_score(y_test,predict)\n",
    "    recall=recall_score(y_test,predict)\n",
    "    print('测试准确率：{:.4f}'.format(score))\n",
    "    print('测试精确率：{:.4f}'.format(precision))\n",
    "    print('测试召回率：{:.4f}'.format(recall))\n",
    "    \n",
    "    end = time.time()\n",
    "    duration = end - start\n",
    "    print('模型训练耗时：{:6f}s'.format(duration))\n",
    "    \n",
    "    \n",
    "    return clf, score,precision,recall, duration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 342,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练LR\n",
      "训练准确率：0.5718\n",
      "测试准确率：0.5634\n",
      "测试精确率：0.5541\n",
      "测试召回率：0.6557\n",
      "模型训练耗时：0.347799s\n",
      "训练DT\n",
      "训练准确率：0.6799\n",
      "测试准确率：0.5792\n",
      "测试精确率：0.5921\n",
      "测试召回率：0.5130\n",
      "模型训练耗时：0.474414s\n",
      "训练AdaBoost\n",
      "训练准确率：0.6270\n",
      "测试准确率：0.6006\n",
      "测试精确率：0.6071\n",
      "测试召回率：0.5737\n",
      "模型训练耗时：2.805242s\n",
      "训练GBDT\n",
      "训练准确率：0.6584\n",
      "测试准确率：0.6051\n",
      "测试精确率：0.6108\n",
      "测试召回率：0.5828\n",
      "模型训练耗时：10.549889s\n",
      "训练RF\n",
      "训练准确率：0.9998\n",
      "测试准确率：0.6061\n",
      "测试精确率：0.6030\n",
      "测试召回率：0.6248\n",
      "模型训练耗时：6.624757s\n",
      "训练XGBoost\n",
      "[13:36:46] WARNING: C:/Users/Administrator/workspace/xgboost-win64_release_1.3.0/src/learner.cc:1061: Starting in XGBoost 1.3.0, the default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'. Explicitly set eval_metric if you'd like to restore the old behavior.\n",
      "训练准确率：0.8753\n",
      "测试准确率：0.6082\n",
      "测试精确率：0.6087\n",
      "测试召回率：0.6091\n",
      "模型训练耗时：1.755881s\n"
     ]
    }
   ],
   "source": [
    "model_name_param_dict = {    'LR': (LogisticRegression(penalty =\"l2\")),\n",
    "                             'DT': (DecisionTreeClassifier(max_depth=10,min_samples_split=10)),\n",
    "                             'AdaBoost': (AdaBoostClassifier()),\n",
    "                             'GBDT': (GradientBoostingClassifier()),\n",
    "                             'RF': (RandomForestClassifier()),\n",
    "                             'XGBoost':(XGBClassifier())\n",
    "                         }\n",
    "\n",
    "result_df = pd.DataFrame(columns=['Accuracy (%)','precision(%)','recall(%)','Time (s)'],\n",
    "                             index=list(model_name_param_dict.keys()))\n",
    "\n",
    "for model_name, model in model_name_param_dict.items():\n",
    "    clf, acc,pre,recall, mean_duration = train_model(X_train, y_train,\n",
    "                                                        X_test, y_test,\n",
    "                                                        model,model_name)\n",
    "    result_df.loc[model_name, 'Accuracy (%)'] = acc\n",
    "    result_df.loc[model_name, 'precision(%)'] = pre\n",
    "    result_df.loc[model_name, 'recall(%)'] = recall\n",
    "    result_df.loc[model_name, 'Time (s)'] = mean_duration \n",
    "\n",
    "result_df.to_csv(os.path.join('model_comparison.csv'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 小结\n",
    "本项目初步尝试了对用户复购行为进行预测,模型还有待进一步做优化."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
